package com.dly.blog.msg;

import com.dly.blog.exception.GlobalException;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.NoArgsConstructor;

import java.io.Serializable;
import java.util.LinkedHashMap;

/**
 * @Author: DaiLiYu
 * @Date: 2023/7/11 10:36
 */
@NoArgsConstructor
public class R<T>  implements Serializable {

    @Schema(description = "业务状态")
    private int code;

    @Schema(description = "状态消息")
    private String msg;

    @Schema(description = "返回内容")
    private T data;

    public R(int code, String msg, T data) {
        this.setCode(code);
        this.setMsg(msg);
        this.setData(data);
    }


    public Integer getCode() {
        return code;
    }

    public String getMsg() {
        return msg;
    }

    public T getData() {
        return data;
    }

    public R setCode(int code) {
        this.code = code;
        return this;
    }

    public R setMsg(String msg) {
        this.msg = msg;
        return this;
    }

    public R setData(T data) {
        this.data = data;
        return this;
    }

    public static R<Void> error(int code, String errorMsg){
        final R<Void> r = new R<>();
        r.setMsg(errorMsg);
        r.setCode(code);
        return r;
    }

    public static R<Void> error(GlobalException e){
        final R<Void> r = new R<>();
        r.setMsg(e.getMessage());
        r.setCode(e.getCode());
        return r;
    }

    public static <T> R<T> ok(T date,String message){
        R<T> r = new R<>();
        r.setMsg(message);
        r.setData(date);
        r.setCode(200);
        return r;
    }

    public static <T> R<T> ok(T date){
        R<T> r = new R<>();
        r.setMsg("成功");
        r.setData(date);
        r.setCode(200);
        return r;
    }

    public static <T> R<T> ok(){
        R<T> r = new R<>();
        r.setMsg("成功");
        r.setCode(200);
        return r;
    }

}
